package com.simple.shiro;

import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * session管理 
 * 测试使用
 */
public class ShiroSessionManager extends DefaultWebSessionManager {
    @Override
    public Serializable getSessionId(SessionKey key) {

        Serializable id=super.getSessionId(key);
        if (id == null && WebUtils.isWeb(key)) {
            ServletRequest request = WebUtils.getRequest(key);
            ServletResponse response = WebUtils.getResponse(key);
            // 如果没有获得到再试图从Cookie中获得
            id = getSessionId(request, response);
        }
        return id;
    }
}
